package com.dongxing.concyrrency.chapter7;

import java.util.concurrent.TimeUnit;

/**
 * @author XavierDong
 **/
public class SynchronizedRunnable implements Runnable {

    private int index = 1;

    // readonly
    private final static int MAX = 500;

    private final Object MONITOR = new Object();

    //this
    @Override
    public void run() {

        while (true) {
            if (ticket()) {
                break;
            }
        }
    }

    private synchronized Boolean ticket() {
        // getFiled
        if (index > MAX) return true;
        try {
            TimeUnit.MILLISECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // index++=>index
        // 1. get Field index
        // 2. index=index+1
        // 3. put field index
        System.out.println(Thread.currentThread() + " 的号码是： " + (index++));
        return false;
    }
}
